{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dd0ce009",
   "metadata": {},
   "source": [
    "# 知识库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b392206",
   "metadata": {},
   "source": [
    "## 1. 正态分布概率密度函数的积分\n",
    "\n",
    "正态分布（Normal Distribution），又称高斯分布，是统计学中非常重要的一种连续型概率分布。其概率密度函数（Probability Density Function, PDF）的完整形式如下：\n",
    "\n",
    "$$ f(x|\\mu,\\sigma^2) = \\frac{1}{\\sqrt{2\\pi}\\sigma} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}} $$\n",
    "\n",
    "其中，$\\mu$ 是均值（mean），$\\sigma^2$ 是方差（variance），$\\sigma$ 是标准差（standard deviation）。\n",
    "\n",
    "积分性质\n",
    "正态分布的概率密度函数在整个实数域 $R$ 上的积分为1，即：\n",
    "\n",
    "$$ \\int_{-\\infty}^{\\infty} f(x|\\mu,\\sigma^2) , dx = 1 $$\n",
    "\n",
    "这一性质确保了概率密度函数能够正确地表示一个概率分布。\n",
    "\n",
    "证明过程\n",
    "为了证明上述积分等于1，我们可以使用变量替换和已知的积分公式。首先，令 $z = \\frac{x - \\mu}{\\sigma}$，则 $dz = \\frac{dx}{\\sigma}$。将 $x$ 和 $dx$ 用 $z$ 和 $dz$ 表示，我们得到：\n",
    "\n",
    "$$ \\int_{-\\infty}^{\\infty} f(x|\\mu,\\sigma^2) , dx = \\int_{-\\infty}^{\\infty} \\frac{1}{\\sqrt{2\\pi}\\sigma} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}} , dx $$\n",
    "\n",
    "$$ = \\frac{1}{\\sqrt{2\\pi}\\sigma} \\int_{-\\infty}^{\\infty} e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}} , dx $$\n",
    "\n",
    "$$ = \\frac{1}{\\sqrt{2\\pi}} \\int_{-\\infty}^{\\infty} e^{-\\frac{z^2}{2}} , dz $$\n",
    "\n",
    "这个积分是一个标准的正态分布的积分，其值为 $\\sqrt{2\\pi}$。因此：\n",
    "\n",
    "$$ \\frac{1}{\\sqrt{2\\pi}} \\int_{-\\infty}^{\\infty} e^{-\\frac{z^2}{2}} , dz = \\frac{1}{\\sqrt{2\\pi}} \\cdot \\sqrt{2\\pi} = 1 $$\n",
    "\n",
    "从而证明了正态分布的概率密度函数在整个实数域上的积分为1。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34b8ff92",
   "metadata": {},
   "source": [
    "## 2. 高斯-欧拉-泊松(Gaussian-Euler-Poisson)积分\n",
    "\n",
    "$$ \\int_{-\\infty}^{\\infty} e^{-{z^2}} dz = \\sqrt{\\pi} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09651f73",
   "metadata": {},
   "source": [
    "## 3. 欧拉积分\n",
    "\n",
    "### 3.1. Γ函数\n",
    "\n",
    "$$ \\Gamma(z)=\\int_0^\\infty x^{z-1}e^{-x}dx \\qquad (s>0)$$\n",
    "$$ \\Gamma(z+1)=z\\Gamma(z) $$\n",
    "$$ \\Gamma(n+1)=n! \\qquad n\\in \\mathbb{N}  $$\n",
    "\n",
    "当 $ 0 < s < 1 $ 时:Γ函数的自变量s与1-s是互余的，因此有(余元公式):\n",
    "$$ \\Gamma(s)\\Gamma(1-s) = \\frac{\\pi}{sin\\pi s} \\qquad (0<s<1) $$\n",
    "\n",
    "特别的，令$s=1/2$时，有:\n",
    "$$ \\Gamma(\\frac{1}{2}) = \\sqrt{\\pi} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8e69314",
   "metadata": {},
   "source": [
    "积分形式：\n",
    "$$ \\Gamma(s) = \\int_0^1 (-log t)^s dt $$\n",
    "$$ \\Gamma(1+\\frac{1}{s}) = \\int_0^{+\\infty} e^{-t^s} dt $$\n",
    "当$s=2$时:\n",
    "$$ \\Gamma(1+\\frac{1}{2}) = \\int_0^{+\\infty} e^{-t^2} dt = \\frac{1}{2} \\sqrt{\\pi} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84d48f75",
   "metadata": {},
   "source": [
    "### 3.2. B函数\n",
    "\n",
    "$$ B(p,q)\t= \\int_0^1 x^{p-1}(1-x)^{q-1}dx \\qquad (p,q>0) $$\n",
    "\n",
    "定义域：$ B(p,q) $的定义域为$p>0,q>0$。\n",
    "连续性：$ B(p,q) $在p>0,q>0内连续。\n",
    "对称性：\n",
    "$$ B(p,q) = B(q,p) $$\n",
    "递推公式：\n",
    "$$ B(p,q) = \\frac{q-1}{p+q+1}B(p-1,q) \\qquad (p>0,q>1)$$\n",
    "$$ B(p,q) = \\frac{p-1}{p+q+1}B(p,q-1) \\qquad (p>1,q>0)$$\n",
    "$$ B(p,q) = \\frac{(p-1)(q-1)}{(p+q-1)(p+q-2)}B(p-1,q-1) \\qquad (p>1,q>1)$$\t\n",
    "\n",
    "其他形式：\n",
    "令$ x = cos^2\\phi $, 则: \n",
    "$$ B(p,q) = 2\\int_0^{\\frac{\\pi}{2}} sin^{2q-1}\\psi cos^{2p-1}\\psi d\\psi $$\n",
    "令$ x = \\frac{y}{1+y}, 1-x = \\frac{1}{1+y} $, 则:\n",
    "$$ B(p,q) = \\int_0^{+\\infty} \\frac{y^{p-1}}{(1+y)^{p+q}}dy $$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "380d8d63",
   "metadata": {},
   "source": [
    "### 3.3. 相互关系\n",
    "\n",
    "$$ B(p,q) = \\frac{\\Gamma(p)\\Gamma(q)}{\\Gamma(p+q)} $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5da02f1",
   "metadata": {},
   "source": [
    "## 4. 欧拉公式\n",
    "\n",
    "$$ e^{ix} = cos(x) + i sin(x) $$\n",
    "\n",
    "$$ e^{ix} = \\begin{bmatrix} cos(x) \\\\ sin(x) \\end{bmatrix} $$\n",
    "\n",
    "欧拉恒等式\n",
    "$$ e^{i\\pi} + 1 = 0 $$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2558a6c9",
   "metadata": {},
   "source": [
    "## 5. 贝塔分布"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "119acad4",
   "metadata": {},
   "source": [
    "### 5.1. 概率密度函数\n",
    "\n",
    "B分布的概率密度函数为：\n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "f(x; \\alpha, \\beta) \n",
    "& = \\frac {x^{\\alpha - 1} (1 - x)^{\\beta - 1}} { \\int_{0}^{1} u^{\\alpha - 1} (1 - u)^{\\beta - 1} du} \\\\\n",
    "& = \\frac {\\Gamma(\\alpha + \\beta)} { \\Gamma(\\alpha) \\Gamma(\\beta) } x^{\\alpha - 1} (1 - x)^{\\beta - 1} \\\\\n",
    "& = \\frac {1} {\\Beta(\\alpha, \\beta)} x^{\\alpha - 1} (1 - x)^{\\beta - 1}\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "随机变量X服从参数为$\\alpha, \\beta$的Β分布通常写作:\n",
    "$$ X \\sim \\Beta(\\alpha, \\beta) $$\n",
    "\n",
    "其中，$\\alpha$和$\\beta$是大于0的实数，且$\\alpha > \\beta$。\n",
    "\n",
    "<div style = \"text-align:center; padding: 10px; background-color: #F2F2F2; border-radius: 10px; width: 500px; height: 500px;\">\n",
    "\t<img src='..\\..\\public\\ai-images\\beta_pdf.webp' styles=\"width: 100%; height: 100%;\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e6b0836",
   "metadata": {},
   "source": [
    "### 5.2. 累积分布函数\n",
    "\n",
    "B分布的累积分布函数是：\n",
    "\n",
    "$$\n",
    "\\begin{aligned} \n",
    "F(x; \\alpha, \\beta) \n",
    "& = \\int_{0}^{1} f(x; \\alpha, \\beta) dx  \\\\\n",
    "& = \\frac{\\Beta_x(\\alpha, \\beta)}{\\Beta(\\alpha, \\beta)} \\\\\n",
    "& = I_x(\\alpha, \\beta)\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n",
    "其中$B_x(\\alpha, \\beta)$是不完全B函数，$I_x(\\alpha, \\beta)$是正则不完全B函数。\n",
    "\n",
    "<div style = \"text-align:center; padding: 10px; background-color: #F2F2F2; border-radius: 10px; width: 500px; height: 500px;\">\n",
    "\t<img src='..\\..\\public\\ai-images\\beta_cdf.webp' styles=\"width: 100%; height: 100%;\">\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95d2aee4",
   "metadata": {},
   "source": [
    "## 5.3. B分布的性质\n",
    "\n",
    "1. 参数为$\\alpha, \\beta$的B分布的众数为：\n",
    "\n",
    "$$ \\frac {\\alpha-1} {\\alpha+\\beta-2} $$\n",
    "\n",
    "2. 均值和方差：\n",
    "\n",
    "$$ \\mu = E(X) = \\frac {\\alpha} {\\alpha+\\beta} $$\n",
    "\n",
    "$$ Var(X) = E(X-\\mu)^2 = \\frac {\\alpha \\beta} {(\\alpha+\\beta)^2 (\\alpha+\\beta+1)} $$\n",
    "\n",
    "3. 偏度：\n",
    "\n",
    "$$ \\gamma = \\frac {E(X-\\mu)^3} {[E(X-\\mu)^2]^{3/2}} = \\frac {2(\\beta-\\alpha)\\sqrt{\\alpha+\\beta+1}} {(\\alpha+\\beta+2)\\sqrt{\\alpha \\beta}} $$\n",
    "\n",
    "4. 峰度：\n",
    "\n",
    "$$ \\delta = \\frac {E(X-\\mu)^4} {[E(X-\\mu)^2]^2} - 3 = \\frac {6[\\alpha^3-\\alpha^2(2\\beta-1)+\\beta^2(\\beta+1)-2\\alpha\\beta(\\beta+2)]} {\\alpha\\beta(\\alpha+\\beta+2)(\\alpha+\\beta+3)} , $$\n",
    "$$ 或\\frac {6[(\\alpha+\\beta)^2(\\alpha+\\beta+1)-\\alpha\\beta(\\alpha+\\beta+2)]} {\\alpha\\beta(\\alpha+\\beta+2)(\\alpha+\\beta+3)} $$\n",
    "\n",
    "5. $k$阶矩：\n",
    "\n",
    "$$ E(X^k) = \\frac {\\Beta(\\alpha+k, \\beta)} {\\Beta(\\alpha, \\beta)} = \\frac {(\\alpha)_k} {(\\alpha+\\beta)_k} $$\n",
    "\n",
    "其中$(x)_k$表示下降阶乘幂。$k$阶矩还可以递归地表示为：\n",
    "\n",
    "$$ E(X^k) = \\frac {\\alpha+k-1} {\\alpha+\\beta+k-1} E\\left(X^{k-1}\\right) $$\n",
    "\n",
    "可以推导出：\n",
    "\n",
    "$$ \\frac {\\beta} {\\alpha + k} = \\frac {E(X^k)} {E(X^{k+1})} - 1$$\n",
    "\n",
    "所以：\n",
    "\n",
    "$$ \\alpha = \\frac {E(X^{k+1})^2 - E(X^{k+1})E(X^{k+2})} {E(X^{k})E(X^{k+2}) - E(X^{k+1})^2} - k $$\n",
    "$$ \\beta = \\left[\\frac {E(X^{k})} {E(X^{k+1})} - 1\\right](\\alpha+k) = \\frac {[E(X^{k}) - E(X^{k+1})][E(X^{k+1}) - E(X^{k+2})]} {E(X^{k})E(X^{k+2}) - E(X^{k+1})^2} $$\n",
    "\n",
    "将k=2代入：\n",
    "\n",
    "$$ \\alpha = \\frac {E(X^3)^2 -E(X^3)E(X^4)} {E(X^2)E(X^4)-E(X^3)^2} - 2 $$\n",
    "$$ \\beta = \\frac {E(X^2)E(X^3)} {E(X^2)E(X^4)-E(X^3)^2} $$\n",
    "\n",
    "6.\n",
    "\n",
    "$$ E(log{X}) = \\psi(\\alpha) - \\psi(\\alpha+\\beta) $$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
